package com.order.rabbitmq.consumer;

import com.common.handler.ExchangeServiceHandler;
import com.order.service.IOrderService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.stereotype.Component;

/**
 * 支付服务消费者
 */
@Component
@Slf4j
public class OrderConsumer {

    @Resource
    IOrderService iOrderService;

    /**
     * 支付服务 -》订单服务 -》订单支付成功
     * @param orderNo
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "order-pay-success-queue", durable = "true", arguments = @Argument(name = "x-queue-mode", value = "lazy")), // 持久化队列 开启惰性队列
            exchange = @Exchange(name = ExchangeServiceHandler.ORDER_EXCHANGE),
            key = "order.pay.success"
    ))
    public void orderPaySuccess(String orderNo){
        log.info("订单服务：订单 {} 已完成支付宝支付",orderNo);
        iOrderService.paySuccess(orderNo);
    }

    /**
     * 支付服务 -》订单服务 -》订单退款成功
     * @param orderNo
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "order-refund-success-queue", durable = "true", arguments = @Argument(name = "x-queue-mode", value = "lazy")), // 持久化队列 开启惰性队列
            exchange = @Exchange(name = ExchangeServiceHandler.ORDER_EXCHANGE),
            key = "order.refund.success"
    ))
    public void orderRefundSuccess(String orderNo){
        log.info("订单服务：订单 {} 退款成功",orderNo);
        iOrderService.refundSuccess(orderNo);
    }

    /**
     * 支付服务 -》订单服务 -》订单退款失败
     * @param orderNo
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "order-refund-fail-queue", durable = "true", arguments = @Argument(name = "x-queue-mode", value = "lazy")), // 持久化队列 开启惰性队列
            exchange = @Exchange(name = ExchangeServiceHandler.ORDER_EXCHANGE),
            key = "order.refund.fail"
    ))
    public void orderRefundFail(String orderNo){
        log.warn("订单服务：订单 {} 退款失败",orderNo);
        iOrderService.refundFail(orderNo);
    }


}
